package com.aries.ui.widget.demo.module.radius;

import com.aries.ui.view.radius.RadiusEditText;
import com.aries.ui.view.radius.RadiusRadioButton;
import com.aries.ui.view.radius.RadiusSwitch;
import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.view.radius.delegate.RadiusViewDelegate;
import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.base.BaseAbilitySlice;
import com.aries.ui.widget.demo.util.SizeUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

/**
 * @Author:
 * @E-Mail:
 * @Function: RadiusView示例
 * @Description:
 */
public class RadiusAbility extends BaseAbilitySlice {
    private Text tvShape;
    private RadiusTextView rtvJavaBg;
    private RadiusTextView rtvDrawable;
    private RadiusTextView rtvSelect;
    private RadiusEditText mRetCircle;
    private Image img1,img2,iv_portrait;
    private ShapeElement mBgDrawable = new ShapeElement();
    private RadiusRadioButton mRadioButtonTest;
    private RadiusSwitch radiusSwitch;
    private RadiusSwitch radiusSwitch2;
    private RadiusEditText ret_radius;

    public RadiusEditText getRet_radius() {
        return ret_radius;
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("RadiusView示例").setStatusBackgroundColor(RgbPalette.WHITE.asArgbInt());
    }

    @Override
    protected void initView(Intent intent) {
        tvShape = (Text) findComponentById(ResourceTable.Id_tv_shapeRadius);
        rtvJavaBg = (RadiusTextView)findComponentById(ResourceTable.Id_rtv_javaBg);

        rtvSelect =  (RadiusTextView)findComponentById(ResourceTable.Id_rtv_selectRadius);
        rtvDrawable = (RadiusTextView)findComponentById(ResourceTable.Id_rtv_drawableRadius);
        mRadioButtonTest = (RadiusRadioButton)findComponentById(ResourceTable.Id_radioTest_radius);
        mRetCircle = (RadiusEditText)findComponentById(ResourceTable.Id_ret_circle);
        ret_radius = (RadiusEditText)findComponentById(ResourceTable.Id_ret_radius);
        img1 = (Image)findComponentById(ResourceTable.Id_img1);
        img2 = (Image)findComponentById(ResourceTable.Id_img2);
        iv_portrait = (Image)findComponentById(ResourceTable.Id_iv_portrait);
        radiusSwitch = (RadiusSwitch)findComponentById(ResourceTable.Id_radiusSwitch);
        radiusSwitch2 = (RadiusSwitch)findComponentById(ResourceTable.Id_radiusSwitch2);
        radiusSwitch.setChecked(true);
        radiusSwitch2.setChecked(true);
        rtvJavaBg.requestFocus();
        initView();
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_radius;
    }

    @Override
    public void onStart(Intent intent) {
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        super.onStart(intent);
    }



    protected void initView( ) {

        //系统shape.xml无法设置虚线，所以用java实现了
        ShapeElement shapeElement  =(ShapeElement) tvShape.getBackgroundElement();
        if (shapeElement!=null){
            shapeElement.setDashPathEffectValues(new float[]{SizeUtil.dp2px(10),SizeUtil.dp2px(5)},0);
        }
        tvShape.setBackground(shapeElement);
        // Img 设置vector图片
        VectorElement vectorElement = new VectorElement(getAbility(),ResourceTable.Media_ic_logo);
       img1.setBackground(vectorElement);
        VectorElement vectorElement2 = new VectorElement(getAbility(),ResourceTable.Media_ic_logo);
       img2.setBackground(vectorElement2);
        VectorElement vectorElement3 = new VectorElement(getAbility(),ResourceTable.Media_ic_logo);
       iv_portrait.setBackground(vectorElement3);


        //以下是通过RadiusViewDelegate代理类设置达到相同效果大家可参照下依然是通过设置GradientDrawable属性
        rtvJavaBg.getDelegate()
                .setTextCheckedColor(Color.BLUE.getValue())
                .setBackgroundCheckedColor(Color.WHITE.getValue())
                .setRadius(2)
                .setStrokeWidth(SizeUtil.dp2px(2))
                .setStrokeColor(Color.getIntColor("#ffaa66cc"))
                .setStrokeDashWidth(SizeUtil.dp2px(10))
                .setStrokeDashGap(SizeUtil.dp2px(5))
                .init();
        rtvSelect.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                rtvSelect.setSelected(!rtvSelect.isSelected());
            }
        });
        rtvDrawable.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                rtvDrawable.setSelected(!rtvDrawable.isSelected());
            }
        });
       rtvDrawable.getDelegate()
                .setLeftDrawable(null)
                .setOnSelectedChangeListener(new RadiusViewDelegate.OnSelectedChangeListener() {
                    @Override
                    public void onSelectedChanged(Component view, boolean isSelected) {
                        showToast("isSelected:" + isSelected);
                    }
                }).init();

       RadiusRadioButton radiusSwitch = new RadiusRadioButton(getAbility());
        radiusSwitch.getDelegate()
                .setBackgroundSelectedColor(Color.BLUE.getValue())
                .setBackgroundCheckedColor(Color.YELLOW.getValue())
                .setRadius(4f)
                .init();
       mRadioButtonTest.getDelegate()
                .setButtonPressedDrawable(ResourceTable.Media_ic_cb_normal)
                .init();

        mRetCircle.setText("我就试一试光标位置");

        mRetCircle.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean b) {
            }
        });
        mRetCircle.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
            @Override
            public void onComponentStateChanged(Component component, int i) {
            }
        });
    }


    private void showToast(String msg){
        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM)
                .setAutoClosable(true)
                .setDuration(1000)
                .setText(msg)
                .setSize(500,100)
                .show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mBgDrawable!=null&&rtvJavaBg!=null){
            rtvJavaBg.setBackground(null);
            mBgDrawable =null;
        }
    }




}
